import { FormGroup } from '@angular/forms';

/**
 * Form工具类
 *
 * @export
 */
export class FormUtils {


  /**
   * 触发form校验
   *
   * @param form form
   */
  public static triggerFormValidate(form: FormGroup): void {
    if (!form){
      return;
    }
    for (const field in form.controls) {
      if (form.controls.hasOwnProperty(field)) {
        form.controls[field].markAsDirty();
        form.controls[field].updateValueAndValidity();
      }
    }
  }


  /**
   * 清空Form
   *
   * @param form form
   */
  public static clearForm(form: FormGroup): void {
    if (!form){
      return;
    }
    for (const field in form.controls) {
      if (form.controls.hasOwnProperty(field)) {
        form.controls[field].setValue(null);
      }
    }
  }

}
